﻿/*
 * Разработчик: Коледа Андрей
 * Дата: 17.03.2011
 * Время: 22:05
 * Email: koleda.andrey@gmail.com
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FastTravel.DataAccess.Interface;
using FastTravel.DB;
using StructureMap;

namespace FastTravel.BusinessLogic
{
	/// <summary>
	/// Класс для управления фирмами (принимающая туриста сторона)
	/// </summary>
	public class TravelAgencyManager
	{
		/// <summary>
		/// Хранилище фирм
		/// </summary>
		private static IRepository<TravelAgency> TravelAgencyRepository
		{
			get { return ObjectFactory.GetInstance<IRepository<TravelAgency>>(); }
		}


		/// <summary>
		/// Добавить новое туристическое агенство
		/// </summary>
		/// <param name="address1">Адресная строка 1</param>
		/// <param name="address2">Адресная строка 2</param>
		/// <param name="description">Описание</param>
		/// <param name="email">Email</param>
		/// <param name="name">Название компании</param>
		/// <param name="phone1">Телефон 1</param>
		/// <param name="phone2">Телефон 2</param>
		/// <param name="url">Адрес сайта</param>
		/// <returns>Агенство</returns>
		public static TravelAgency Add(string address1, string address2, string description, string email, 
			string name, int phone1, int phone2, string url)
		{
			if (!string.IsNullOrWhiteSpace(name))
			{
				var travelAgency = new TravelAgency
				                   	{
				                   		AddedDate = DateTime.Now,
				                   		AddressLine1 = address1,
				                   		AddressLine2 = address2,
				                   		Description = description,
				                   		Email = email,
				                   		Name = name,
				                   		Phone1 = phone1,
				                   		Phone2 = phone2,
				                   		URL = url
				                   	};

				TravelAgencyRepository.Add(travelAgency);
				return travelAgency;
			}
			return null;
		}

		/// <summary>
		/// Получить агенство
		/// </summary>
		/// <param name="id">ID агенства</param>
		/// <returns>Агенство</returns>
		public static TravelAgency Get(int id)
		{
			return TravelAgencyRepository.Single(x => x.ID == id);
		}

		/// <summary>
		/// Получить страницу агенств
		/// </summary>
		/// <param name="start">Начальная страница</param>
		/// <param name="limit">Размер страницы</param>
		/// <param name="sort">Сортировка по полю</param>
		/// <param name="dir">Направление сортировки</param>
		/// <param name="name">Фильтр: Название агенства</param>
		/// <param name="description">Фильтр: Описание агенства</param>
		/// <param name="address1">Фильтр: Адресная строка 1</param>
		/// <param name="address2">Фильтр: Адресная строка 2</param>
		/// <param name="count">Общее число агенств</param>
		/// <returns>Список агенств</returns>
		public static List<TravelAgency> GetPage(int start, int limit, string sort, string dir, string name, 
			string description, string address1, string address2, out int count)
		{
			var query = TravelAgencyRepository.GetQuery().Select(e => e);

			if (!string.IsNullOrEmpty(name))
			{
				query = query.Where(x => x.Name.ToLower().StartsWith(name.ToLower()));
			}

			if (!string.IsNullOrEmpty(description))
			{
				query = query.Where(x => x.Description.ToLower().Contains(description));
			}

			if (!string.IsNullOrEmpty(address1))
			{
				query = query.Where(x => x.AddressLine1.ToLower().Contains(address1));
			}

			if (!string.IsNullOrEmpty(address2))
			{
				query = query.Where(x => x.AddressLine2.ToLower().Contains(address2));
			}

			count = query.Count();

			if (!string.IsNullOrEmpty(sort))
			{
				query = dir == "ASC"
							? TravelAgencyRepository.SortByQuery<TravelAgency>(query, sort, true)
							: TravelAgencyRepository.SortByQuery<TravelAgency>(query, sort, false);
			}

			if (start >= 0 && limit > 0)
			{
				query = query.Skip(start).Take(limit);
			}
			return query.ToList();
		}

		/// <summary>
		/// Удалить агенство
		/// </summary>
		/// <param name="id">ID</param>
		public static void Delete(int id)
		{
			TravelAgency travelAgency = TravelAgencyRepository.Single(x => x.ID == id);
			if (travelAgency != null)
			{
				TravelAgencyRepository.Delete(travelAgency);
			}
		}

		/// <summary>
		/// Обновить данные об агенстве
		/// </summary>
		/// <param name="description">Описание</param>
		/// <param name="email">Email</param>
		/// <param name="name">Название</param>
		/// <param name="agencyId">ID агенства</param>
		/// <param name="address1">Адрес 1</param>
		/// <param name="address2">Адрес 2</param>
		/// <param name="phone1">Телефон 1</param>
		/// <param name="phone2">Телефон 2</param>
		/// <param name="url">адрес сайта</param>
		public static void Update(int agencyId, string address1, string address2, string description, string email,
			string name, int phone1, int phone2, string url)
		{
			TravelAgency travelAgency = TravelAgencyRepository.Single(x => x.ID == agencyId);
			if (travelAgency != null)
			{
				travelAgency.AddressLine1 = address1;
				travelAgency.AddressLine2 = address2;
				travelAgency.Description = description;
				travelAgency.Email = email;
				travelAgency.Name = name;
				travelAgency.Phone1 = phone1;
				travelAgency.Phone2 = phone2;
				travelAgency.URL = url;

				TravelAgencyRepository.SaveChanges();
			}
		}



	}
}
